package com.kaikeba.controller;

import com.kaikeba.bean.Message;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.AdminService;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class AdminController {

    @ResponseBody(value = "/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean result = AdminService.login(username, password);
        Message msg = null;
        if(result){
            req.getSession().setAttribute("adminUsername", username);
            msg = new Message(0, "登录成功");
            Date date = new Date();
            String ip = req.getRemoteAddr();
            AdminService.updateLoginTimeAndIp(username, date, ip);
        }else{
            msg = new Message(-1, "登录失败");
        }
        return JSONUtil.toJson(msg);
    }

}
